package com.ktg.common.core.domain.model;

import org.springframework.transaction.interceptor.TransactionAspectSupport;

public class SqlResult {

    private Integer code = -1;

    private String msg;

    private Object data;

    public Integer getCode() {
        return code;
    }

    public SqlResult setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public SqlResult setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getData() {
        return data;
    }

    public SqlResult setData(Object data) {
        this.data = data;
        return this;
    }

    public static SqlResult code(Integer code, String msg, Object data){
        SqlResult sqlResult = new SqlResult();
        sqlResult.code = code;
        sqlResult.msg = msg;
        sqlResult.data = data;
        return sqlResult;
    }


    public static SqlResult ok(String msg,Object data){
        SqlResult sqlResult = new SqlResult();
        sqlResult.code = 0;
        sqlResult.msg = msg;
        sqlResult.data = data;
        return sqlResult;
    }



    /**
     * 所有的异常都抛出，为@Transactional
     * @param msg
     * @return
     */
    public static SqlResult error(String msg,Object data){
        try {
            //先返回回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();;
        }catch (Exception e){

        }
        SqlResult sqlResult = new SqlResult();
        sqlResult.code = -1;
        sqlResult.msg = msg;
        sqlResult.data = data;
        return sqlResult;
    }

    public boolean isSuccess(){
        if (code == 0){
            return true;
        }
        return false;
    }

}
